package com.zx.erp.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.erp.sys.bean.Module;
import com.zx.erp.sys.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郑攀武
 * @since 2021-11-10
 */
@Controller
@RequestMapping("/sys/modules")
@SessionAttributes({"dlist", "plist", "parentModules", "childModules", "pageInfo"})
public class ModuleController {
    @Autowired
    private ModuleService mbliz;

    @RequestMapping("/list")
    public String list(Model model, Module module, @RequestParam(value = "pageno", defaultValue = "1") Integer
            pageNo){
        Integer pageSize = 6;
        Page<Module> pageInfo = mbliz.moduleAll(module, pageNo, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        List<Module> mlist = mbliz.list();

        model.addAttribute("mlist",mlist);
        return "forward:/sys/modules/moduleList.html";

    }


    @RequestMapping("/toAdd")           /*跳转添加页面*/
    public String toAdd(Model model) {
        List<Module> parentModule = mbliz.findParentModule();
        model.addAttribute("parentModule", parentModule);
        return "forward:/sys/modules/moduleAdd.html";
    }

    @RequestMapping("/moduleAdd")
    public String moduleAdd(Module module){
        boolean b = mbliz.addModule(module);
        if (b) {
            return "redirect:/sys/modules/list";
        } else {
            return "forward:/sys/modules/toAdd";
        }
    }


    @RequestMapping("/toUpdate/{moduleId}")    /*跳转修改页面*/
    public  String toUpdate(@PathVariable("moduleId") Integer moduleId, Model model){
        //查询模块详情
        Module byId = mbliz.seModuleById(moduleId);
        model.addAttribute("byId",byId);
        //查父模块
        List<Module> parentModule = mbliz.findParentModule();
        model.addAttribute("parentModule", parentModule);
        return "forward:/sys/modules/moduleUpdate.html";
    }

    /*修改*/
    @RequestMapping("/updateModule")
    public String updateModule(Module module){
        boolean update = mbliz.updateModule(module);
        if (update) {
            return "redirect:/sys/modules/list";
        } else {
            return "forward:/sys/modules/toUpdate";
        }
    }

    /*查看详情*/
    @RequestMapping("/detailModule/{moduleId}")
    public String detailModule(@PathVariable("moduleId") Integer moduleId,Model model ){
        /*查父模块*/
        List<Module> parentModule = mbliz.findParentModule();
        model.addAttribute("pm",parentModule);
        /*查子模块*/
        Module byId = mbliz.seModuleById(moduleId);
        model.addAttribute("byId",byId);

        return "forward:/sys/modules/moduleView.html";
    }

    @RequestMapping("/doState/{moduleId}")
    public String doState(@PathVariable("moduleId") Integer moduleId){
        Module id = mbliz.seModuleById(moduleId);
        if (id.getModuleState()==1){
            id.setModuleState(2);
        }else{
            id.setModuleState(1);
        }
        boolean b = mbliz.updateModule(id);

        return "redirect:/sys/modules/list";


    }






}

